 package com.rpframework.website.friendly.act.admin;

import java.text.ParseException;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.rpframework.utils.DateUtils;
import com.rpframework.utils.NumberUtils;
import com.rpframework.utils.Pager;
import com.rpframework.website.friendly.domain.News;
import com.rpframework.website.friendly.service.NewsService;
@Controller
@RequestMapping("/admin/news")
public class AdminNewsAct extends AdminAct{
	@Resource NewsService service;
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping("/list")
	public String list(@RequestParam(value="pager", required=false) Pager pager, 
			@RequestParam(value="state", required=false) Integer state, 
			Map<Object, Object> model){
		if(pager == null) {
			pager = new Pager();
		}
		if(NumberUtils.isValid(state)){
			pager.getSearchMap().put("state", state.toString());
			model.put("state", state);
		}
		pager = service.getPager(pager);
		model.put("pager", pager);
		return this.doPackageURI("news/list");
	}
	//this funtion is edit 
		@RequestMapping("/edit-{id}")
		public String edit(@PathVariable Integer id, Map<Object, Object> model,RedirectAttributes attr){
			// get entity by service
			News t = service.select(id);
			Assert.notNull(t,this.getClass().getName() + " Id 异常");
			// model put tablePager and this add redirect page replace modelpager
			model.put("pager", t);
			return this.add(attr, model);
		}
		@RequestMapping("/add")
		public String add(RedirectAttributes attr,Map<Object, Object> model){
			return this.doPackageURI("news/edit");
		}
		
		//this funtion is delete 
		@RequestMapping("/delete-{id}")
		public String delete(@PathVariable Integer id ,RedirectAttributes attr){
			//delete entity by service
			boolean flag = service.delete(id);
			if(flag){
				setInfoMsg("删除成功！", attr);
			}else{
				setInfoMsg("删除失败！", attr);
			}
			return redirect("/admin/news/list");
		}
		
		//this funtion is saveOrUpdate 
		@RequestMapping("/dosave")
		public String doSaveOrUpdate(@ModelAttribute News t,
				RedirectAttributes attr) throws ParseException{
			//service save or update
			t.setPublishTime(t.getPublishTime());
			boolean flag = service.doSaveOrUpdate(t);
			if(flag){
				if(NumberUtils.isValid(t.getId())) {//update
					setInfoMsg("更新操作成功！", attr);
				} else {
					setInfoMsg("新增操作成功！", attr);
				}
			}else{
				setInfoMsg("操作失败！", attr);
			}
			return redirect("/admin/news/list");
		}
		@RequestMapping("/on")
		public @ResponseBody JsonElement onState(@RequestParam(value="id", required=false) Integer id){
			JsonObject json = new JsonObject();
			boolean flag = service.doUpdateState(id);
			if(flag){
				json.addProperty("succ", true);
			}else{
				json.addProperty("succ", false);
			}
			return json;
		}
		@RequestMapping("/off")
		public @ResponseBody JsonElement offState(@RequestParam(value="id", required=false) Integer id){
			JsonObject json = new JsonObject();
			boolean flag = service.doUpdateState(id);
			if(flag){
				json.addProperty("succ", true);
			}else{
				json.addProperty("succ", false);
			}
			return json;
		}
		@RequestMapping("/rec")
		public @ResponseBody JsonElement doRecommender(@RequestParam(value="id", required=false) Integer id){
			JsonObject json = new JsonObject();
			boolean flag = service.getRecommender(id);
			if(flag){
				json.addProperty("succ", true);
			}else{
				json.addProperty("succ", false);
			}
			return json;
		}
}
